// https://www.luogu.com.cn/problem/P2627
// https://soj.turingedu.cn/submission/34591
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad(); // read int
const int max_size = 5 + 100;
const int maxn = 5 + 1e5;

int n, m;
int a[maxn];
ll f[maxn][2];
ll pre[maxn];
/*
f[i][0] = max(f[i - 1][0], f[i - 1][1])
f[i][1] = max(f[j][0] + sum(a[j + 1 ~ i])), j->[i - m, i - 1]
    f[i][1] = max(f[j][0] + pre[i] - pre[j])
            = max(f[j][0] - pre[j]) + pre[i]
*/
struct Node {
    ll val;
    int pos;
};
deque<Node> qe;
void push(ll val, int pos) {
    while (!qe.empty() && qe.back().val < val)
        qe.pop_back();
    while (!qe.empty() && pos - qe.front().pos >= m)
        qe.pop_front();
    qe.push_back({val, pos});
}

int main() {
    n = rad(), m = rad();
    rf(i, n) a[i] = rad(), pre[i] = pre[i - 1] + a[i];

    push(0, 0);
    for (int i = 1; i <= n; ++i) {
        f[i][0] = max(f[i - 1][0], f[i - 1][1]);
        // for (int j = max(0, i - m); j <= i - 1; ++j)
        //     f[i][1] = max(f[i][1], f[j][0] + pre[i] - pre[j]);
        f[i][1] = qe.front().val + pre[i];
        // printf("i = %d, max_pos = %d, max_val = %d, | %d, %d\n", i, qe.front().pos, qe.front().val, f[i][0], f[i][1]);
        push(f[i][0] - pre[i], i);
    }
    printf("%lld\n", max(f[n][0], f[n][1]));
}

int rad() {
    int back = 0, ch = 0, neg = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        neg = ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return (back ^ -neg) + neg;
}